package com.songlanyun.modules.account.controller.admin;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.enums.MemberConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.account.entity.AccountEntity;
import com.songlanyun.modules.account.entity.UserShopRoleEntity;
import com.songlanyun.modules.account.service.UserShopRoleService;
import com.songlanyun.modules.exception.MemberException;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 会员在每个店铺中的等级（会员的上级会员如查成为商家，自己的绑定店铺成为这个商家的店铺，同时之前所在商家的等级要克隆下来）
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-05 17:23:13
 */
@RestController
@RequestMapping("admin/account/userShopRole")
public class UserShopRoleAdminController extends AbstractController {
    @Autowired
    private UserShopRoleService userShopRoleService;

    /**
     * 登录店铺的所有等级设置
     */
    @GetMapping("/getAllByShop")
//    @RequiresPermissions("account:userShopRole:info")
    public R getAllByShop() {
        Long shopId = getShopId();
        List<UserShopRoleEntity> list = userShopRoleService.list(
                new LambdaQueryWrapper<UserShopRoleEntity>()
                        .eq(UserShopRoleEntity::getShopId, shopId)
        );

        return R.ok().put("data", list);
    }

    /**
     * 查询会员在登录店铺的等级设置
     */
    @GetMapping("/getLevelConfigByUser")
//    @RequiresPermissions("account:userShopRole:info")
    public R getLevelConfigByUser(@RequestParam Long userId) {
        Long shopId = getShopId();
        UserShopRoleEntity entity = userShopRoleService.getUserShopRole(shopId, userId);

        return R.ok().put("data", entity);
    }


    /**
     * 修改会员等级
     *
     * @param
     * @return
     */
    @SysLog("修改会员等级")
    @PostMapping("/update-class")
//    @RequiresPermissions("account:yxtaccount:info")
    public R updateClass(@RequestBody Map<String, Object> param) {
        Long shopId = getShopId();
        Long userId = MapUtil.getLong(param, "userId");
        Integer level = MapUtil.getInt(param, "level");

        if (ObjectUtil.isNull(shopId) || shopId == 0L) {
            throw new RRException(MemberException.STORES_CAN_ONLY_MODIFY_MEMBERSHIP_LEVELS);
        }
        if (ObjectUtil.isNull(userId) || ObjectUtil.isNull(level)) {
            throw new RRException(MemberException.INCOMPLETE_PARAMETER);
        }
        if (!(MemberConstant.MemberLevel.GENERAL.getCode() <= level
                && level <= MemberConstant.MemberLevel.PAETICIPANT.getCode())) {
            throw new RRException(MemberException.ABNORMAL_GRADE_PARAMETER);
        }

        userShopRoleService.updateClass(shopId, userId, level);
        return R.ok();
    }
}
